﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fcwms.Common;

namespace fcwms.DAL
{
    /// <summary>
    /// 数据访问类:dt_ruku
    /// </summary>
    public partial class dt_ruku
    {
        public dt_ruku()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dt_ruku");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在报关单号
        /// </summary>
        public bool Exists(string decla_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dt_ruku");
            strSql.Append(" where decla_id=@decla_id");
            SqlParameter[] parameters = new SqlParameter[]{
                    new SqlParameter("@decla_id",decla_id)};
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 返回入库仓库
        /// </summary>
        /// <param name="decla_id"></param>
        /// <returns></returns>
        public string GetRukuStorage(string decla_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 ruku_storage from dt_ruku");
            strSql.Append(" where decla_id=@decla_id ");
            SqlParameter[] parameters = new SqlParameter[] { 
                    new SqlParameter("@decla_id",decla_id)};
            string ruku_storage = DbHelperSQL.GetSingle(strSql.ToString(), parameters).ToString();
            if (string.IsNullOrEmpty(ruku_storage))
            {
                return "";
            }
            return ruku_storage;
        }

        /// <summary>
        /// 增加一条数据,及其子表数据
        /// </summary>
        public int Add(fcwms.Model.dt_ruku model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dt_ruku(");
            strSql.Append("decla_id,ruku_status,ruku_time,ruku_way,ruku_storage,customs_broker,unstuffing_group,tallyman,customer,customs_officer,transactor,remark)");
            strSql.Append(" values (");
            strSql.Append("@decla_id,@ruku_status,@ruku_time,@ruku_way,@ruku_storage,@customs_broker,@unstuffing_group,@tallyman,@customer,@customs_officer,@transactor,@remark)");
            strSql.Append(";set @ReturnValue= @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
                    new SqlParameter("@ruku_time", SqlDbType.DateTime),
					new SqlParameter("@ruku_way", SqlDbType.NVarChar,50),
					new SqlParameter("@ruku_storage", SqlDbType.NVarChar,100),
					new SqlParameter("@customs_broker", SqlDbType.NVarChar,100),
					new SqlParameter("@unstuffing_group", SqlDbType.NVarChar,100),
					new SqlParameter("@tallyman", SqlDbType.NVarChar,100),
					new SqlParameter("@customer", SqlDbType.NVarChar,100),
					new SqlParameter("@customs_officer", SqlDbType.NVarChar,100),
                    new SqlParameter("@transactor", SqlDbType.NVarChar,100),
					new SqlParameter("@remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@ReturnValue",SqlDbType.Int),
                    new SqlParameter("@ruku_status", SqlDbType.Int,4),};
            parameters[0].Value = model.decla_id;
            parameters[1].Value = model.ruku_time;
            parameters[2].Value = model.ruku_way;
            parameters[3].Value = model.ruku_storage;
            parameters[4].Value = model.customs_broker;
            parameters[5].Value = model.unstuffing_group;
            parameters[6].Value = model.tallyman;
            parameters[7].Value = model.customer;
            parameters[8].Value = model.customs_officer;
            parameters[9].Value = model.transactor;
            parameters[10].Value = model.remark;
            parameters[11].Direction = ParameterDirection.Output;
            parameters[12].Value = model.ruku_status;

            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            if (model.dt_goodss != null)
            {
                StringBuilder strSql2;
                foreach (fcwms.Model.dt_goods models in model.dt_goodss)
                {
                    strSql2 = new StringBuilder();
                    strSql2.Append("insert into dt_goods(");
                    strSql2.Append("storage_num,days,cost,cost_status,deal_status,remark,decla_id,goods_name,num,single_num,cube)");
                    strSql2.Append(" values (");
                    strSql2.Append("@storage_num,@days,@cost,@cost_status,@deal_status,@remark,@decla_id,@goods_name,@num,@single_num,@cube)");
                    SqlParameter[] parameters2 = {
						new SqlParameter("@storage_num", SqlDbType.Decimal,9),
						new SqlParameter("@days", SqlDbType.Int,4),
						new SqlParameter("@cost", SqlDbType.Decimal,9),
						new SqlParameter("@cost_status", SqlDbType.Int,4),
						new SqlParameter("@deal_status", SqlDbType.Int,4),
						new SqlParameter("@remark", SqlDbType.NVarChar,200),
						new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
						new SqlParameter("@goods_name", SqlDbType.NVarChar,100),
						new SqlParameter("@num", SqlDbType.Decimal,9),
						new SqlParameter("@single_num", SqlDbType.Decimal,9),
						new SqlParameter("@cube", SqlDbType.Decimal,9)};
                    parameters2[0].Value = models.storage_num;
                    parameters2[1].Value = models.days;
                    parameters2[2].Value = models.cost;
                    parameters2[3].Value = models.cost_status;
                    parameters2[4].Value = models.deal_status;
                    parameters2[5].Value = models.remark;
                    parameters2[6].Value = model.decla_id;
                    parameters2[7].Value = models.goods_name;
                    parameters2[8].Value = models.num;
                    parameters2[9].Value = models.single_num;
                    parameters2[10].Value = models.cube;

                    cmd = new CommandInfo(strSql2.ToString(), parameters2);
                    sqllist.Add(cmd);
                }
            }

            if (model.dt_locations != null)
            {
                StringBuilder strSql3;
                foreach (fcwms.Model.dt_location models in model.dt_locations)
                {
                    strSql3 = new StringBuilder();
                    strSql3.Append("insert into dt_location(");
                    strSql3.Append("decla_id,storage_id,shelf_id,floor_id,line_id,is_exist)");
                    strSql3.Append(" values (");
                    strSql3.Append("@decla_id,@storage_id,@shelf_id,@floor_id,@line_id,@is_exist)");
                    strSql3.Append(";set @ReturnValue= @@IDENTITY");
                    SqlParameter[] parameters3 = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
					new SqlParameter("@storage_id", SqlDbType.NVarChar,50),
					new SqlParameter("@shelf_id", SqlDbType.NVarChar,50),
					new SqlParameter("@floor_id", SqlDbType.NVarChar,50),
					new SqlParameter("@line_id", SqlDbType.NVarChar,50),
                    new SqlParameter("@is_exist", SqlDbType.Int,4),
                    new SqlParameter("@ReturnValue",SqlDbType.Int)};
                    parameters3[0].Value = model.decla_id;
                    parameters3[1].Value = models.storage_id;
                    parameters3[2].Value = models.shelf_id;
                    parameters3[3].Value = models.floor_id;
                    parameters3[4].Value = models.line_id;
                    parameters3[5].Value = models.is_exist;
                    parameters3[6].Direction = ParameterDirection.Output;

                    cmd = new CommandInfo(strSql3.ToString(), parameters3);
                    sqllist.Add(cmd);
                }
            }

            StringBuilder strSql4 = new StringBuilder();
            strSql4.Append("update dt_storage set storage_status=1");
            strSql4.Append("  where storage_name=@storage_name ");
            SqlParameter[] parameters4 = {
                          new SqlParameter("@storage_name",SqlDbType.NVarChar,100)};
            parameters4[0].Value = model.ruku_storage;
            cmd = new CommandInfo(strSql4.ToString(), parameters4);
            sqllist.Add(cmd);


            DbHelperSQL.ExecuteSqlTranWithIndentity(sqllist);
            return (int)parameters[11].Value;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(fcwms.Model.dt_ruku model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update dt_ruku set ");
                        strSql.Append("customs_officer=@customs_officer,");
                        strSql.Append("transactor=@transactor,");
                        strSql.Append("remark=@remark,");
                        strSql.Append("ruku_time=@ruku_time,");
                        strSql.Append("ruku_way=@ruku_way,");
                        strSql.Append("ruku_storage=@ruku_storage,");
                        strSql.Append("customs_broker=@customs_broker,");
                        strSql.Append("unstuffing_group=@unstuffing_group,");
                        strSql.Append("tallyman=@tallyman,");
                        strSql.Append("customer=@customer");
                        strSql.Append(" where decla_id=@decla_id ");
                        SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@customs_officer", SqlDbType.NVarChar,100),
					new SqlParameter("@transactor", SqlDbType.NVarChar,100),
					new SqlParameter("@remark", SqlDbType.NVarChar,200),
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
					new SqlParameter("@ruku_time", SqlDbType.DateTime),
					new SqlParameter("@ruku_way", SqlDbType.NVarChar,50),
					new SqlParameter("@ruku_storage", SqlDbType.NVarChar,100),
					new SqlParameter("@customs_broker", SqlDbType.NVarChar,100),
					new SqlParameter("@unstuffing_group", SqlDbType.NVarChar,100),
					new SqlParameter("@tallyman", SqlDbType.NVarChar,100),
					new SqlParameter("@customer", SqlDbType.NVarChar,100)};
                        parameters[0].Value = model.id;
                        parameters[1].Value = model.customs_officer;
                        parameters[2].Value = model.transactor;
                        parameters[3].Value = model.remark;
                        parameters[4].Value = model.decla_id;
                        parameters[5].Value = model.ruku_time;
                        parameters[6].Value = model.ruku_way;
                        parameters[7].Value = model.ruku_storage;
                        parameters[8].Value = model.customs_broker;
                        parameters[9].Value = model.unstuffing_group;
                        parameters[10].Value = model.tallyman;
                        parameters[11].Value = model.customer;
                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);

                        //删除已删除的菜单
                        new fcwms.DAL.dt_goods().DeleteList2(conn, trans, model.dt_goodss, model.decla_id);


                        //添加/修改菜单
                        if (model.dt_goodss != null)
                        {
                            StringBuilder strSql2;
                            foreach (Model.dt_goods models in model.dt_goodss)
                            {
                                strSql2 = new StringBuilder();
                                if (models.id > 0)
                                {
                                    strSql2.Append("update dt_goods set ");
                                    strSql2.Append("decla_id=@decla_id,");
                                    strSql2.Append("goods_name=@goods_name,");
                                    strSql2.Append("num=@num,");
                                    strSql2.Append("single_num=@single_num,");
                                    strSql2.Append("cube=@cube,");
                                    strSql2.Append("storage_num=@storage_num,");
                                    strSql2.Append("days=@days,");
                                    strSql2.Append("cost=@cost,");
                                    strSql2.Append("cost_status=@cost_status,");
                                    strSql2.Append("deal_status=@deal_status,");
                                    strSql2.Append("remark=@remark");
                                    strSql2.Append(" where id=@id");
                                    SqlParameter[] parameters2 = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
					new SqlParameter("@goods_name", SqlDbType.NVarChar,100),
					new SqlParameter("@num", SqlDbType.Decimal,9),
					new SqlParameter("@single_num", SqlDbType.Decimal,9),
					new SqlParameter("@cube", SqlDbType.Decimal,9),
					new SqlParameter("@storage_num", SqlDbType.Decimal,9),
					new SqlParameter("@days", SqlDbType.Int,4),
					new SqlParameter("@cost", SqlDbType.Decimal,9),
					new SqlParameter("@cost_status", SqlDbType.Int,4),
					new SqlParameter("@deal_status", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.NVarChar,200),
					new SqlParameter("@id", SqlDbType.Int,4)};
                                    parameters2[0].Value = model.decla_id;
                                    parameters2[1].Value = models.goods_name;
                                    parameters2[2].Value = models.num;
                                    parameters2[3].Value = models.single_num;
                                    parameters2[4].Value = models.cube;
                                    parameters2[5].Value = models.storage_num;
                                    parameters2[6].Value = models.days;
                                    parameters2[7].Value = models.cost;
                                    parameters2[8].Value = models.cost_status;
                                    parameters2[9].Value = models.deal_status;
                                    parameters2[10].Value = models.remark;
                                    parameters2[11].Value = models.id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql2.ToString(), parameters2);
                                }
                                else
                                {
                                    strSql2.Append("insert into dt_goods(");
                                    strSql2.Append("storage_num,days,cost,cost_status,deal_status,remark,decla_id,goods_name,num,single_num,cube)");
                                    strSql2.Append(" values (");
                                    strSql2.Append("@storage_num,@days,@cost,@cost_status,@deal_status,@remark,@decla_id,@goods_name,@num,@single_num,@cube)");
                                    SqlParameter[] parameters2 = {
						new SqlParameter("@storage_num", SqlDbType.Decimal,9),
						new SqlParameter("@days", SqlDbType.Int,4),
						new SqlParameter("@cost", SqlDbType.Decimal,9),
						new SqlParameter("@cost_status", SqlDbType.Int,4),
						new SqlParameter("@deal_status", SqlDbType.Int,4),
						new SqlParameter("@remark", SqlDbType.NVarChar,200),
						new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
						new SqlParameter("@goods_name", SqlDbType.NVarChar,100),
						new SqlParameter("@num", SqlDbType.Decimal,9),
						new SqlParameter("@single_num", SqlDbType.Decimal,9),
						new SqlParameter("@cube", SqlDbType.Decimal,9)};
                                    parameters2[0].Value = models.storage_num;
                                    parameters2[1].Value = models.days;
                                    parameters2[2].Value = models.cost;
                                    parameters2[3].Value = models.cost_status;
                                    parameters2[4].Value = models.deal_status;
                                    parameters2[5].Value = models.remark;
                                    parameters2[6].Value = model.decla_id;
                                    parameters2[7].Value = models.goods_name;
                                    parameters2[8].Value = models.num;
                                    parameters2[9].Value = models.single_num;
                                    parameters2[10].Value = models.cube;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql2.ToString(), parameters2);
                                }
                            }
                        }

                        //删除已删除的菜单
                        new fcwms.DAL.dt_location().DeleteList2(conn, trans, model.dt_locations, model.decla_id);

                        //添加/修改菜单
                        if (model.dt_locations != null)
                        {
                            StringBuilder strSql3;
                            foreach (Model.dt_location models in model.dt_locations)
                            {
                                strSql3 = new StringBuilder();
                                if (models.id > 0)
                                {
                                    strSql3.Append("update dt_location set ");
                                    strSql3.Append("decla_id=@decla_id,");
                                    strSql3.Append("storage_id=@storage_id,");
                                    strSql3.Append("shelf_id=@shelf_id,");
                                    strSql3.Append("floor_id=@floor_id,");
                                    strSql3.Append("line_id=@line_id,");
                                    strSql3.Append("is_exist=@is_exist");
                                    strSql3.Append(" where id=@id");
                                    SqlParameter[] parameters3 = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
					new SqlParameter("@storage_id", SqlDbType.NVarChar,50),
					new SqlParameter("@shelf_id", SqlDbType.NVarChar,50),
					new SqlParameter("@floor_id", SqlDbType.NVarChar,50),
					new SqlParameter("@line_id", SqlDbType.NVarChar,50),
					new SqlParameter("@is_exist", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
                                    parameters3[0].Value = model.decla_id;
                                    parameters3[1].Value = models.storage_id;
                                    parameters3[2].Value = models.shelf_id;
                                    parameters3[3].Value = models.floor_id;
                                    parameters3[4].Value = models.line_id;
                                    parameters3[5].Value = models.is_exist;
                                    parameters3[6].Value = models.id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql3.ToString(), parameters3);
                                }
                                else
                                {
                                    strSql3.Append("insert into dt_location(");
                                    strSql3.Append("decla_id,storage_id,shelf_id,floor_id,line_id,is_exist)");
                                    strSql3.Append(" values (");
                                    strSql3.Append("@decla_id,@storage_id,@shelf_id,@floor_id,@line_id,@is_exist)");

                                    SqlParameter[] parameters3 = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
					new SqlParameter("@storage_id", SqlDbType.NVarChar,50),
					new SqlParameter("@shelf_id", SqlDbType.NVarChar,50),
					new SqlParameter("@floor_id", SqlDbType.NVarChar,50),
					new SqlParameter("@line_id", SqlDbType.NVarChar,50),
					new SqlParameter("@is_exist", SqlDbType.Int,4)};
                                    parameters3[0].Value = model.decla_id;
                                    parameters3[1].Value = models.storage_id;
                                    parameters3[2].Value = models.shelf_id;
                                    parameters3[3].Value = models.floor_id;
                                    parameters3[4].Value = models.line_id;
                                    parameters3[5].Value = models.is_exist;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql3.ToString(), parameters3);
                                }
                            }
                        }


                        trans.Commit();
                    }
                    catch
                    {
                        //trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        //更新入库/仓库状态
        public bool UpdateStatus(fcwms.Model.dt_ruku model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dt_ruku set ");
            strSql.Append("ruku_status=@ruku_status");
            strSql.Append(" where decla_id=@decla_id ");
            SqlParameter[] parameters = {
                           new SqlParameter("@ruku_status",SqlDbType.Int,4),
                           new SqlParameter("@decla_id",SqlDbType.NVarChar,100)};
            parameters[0].Value = 1;
            parameters[1].Value = model.decla_id;

            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("update dt_storage set ");
            strSql2.Append("storage_status=@storage_status");
            strSql2.Append(" where storage_name=@storage_name");
            SqlParameter[] parameters2 = {
                           new SqlParameter("@storage_status",SqlDbType.Int,4),
                           new SqlParameter("@storage_name",SqlDbType.NVarChar,100)};
            parameters2[0].Value = 0;
            parameters2[1].Value = model.ruku_storage;

            cmd = new CommandInfo(strSql2.ToString(), parameters2);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据，及子表所有相关数据
        /// </summary>
        public bool Delete(string decla_id)
        {
            List<CommandInfo> sqllist = new List<CommandInfo>();

            //删除入库信息表
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete dt_ruku ");
            strSql.Append(" where decla_id=@decla_id");
            SqlParameter[] parameters = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100)
			};
            parameters[0].Value = decla_id;
            CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            //删除物品信息表
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("delete dt_goods ");
            strSql2.Append(" where decla_id=@decla_id ");
            SqlParameter[] parameters2 = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100)};
            parameters2[0].Value = decla_id;
            cmd = new CommandInfo(strSql2.ToString(), parameters2);
            sqllist.Add(cmd);

            //删除库位信息表
            StringBuilder strSql3 = new StringBuilder();
            strSql3.Append("delete dt_location ");
            strSql3.Append(" where decla_id=@decla_id ");
            SqlParameter[] parameters3 = { 
                    new SqlParameter("@decla_id",SqlDbType.NVarChar,100)};
            parameters3[0].Value = decla_id;
            cmd = new CommandInfo(strSql3.ToString(), parameters3);
            sqllist.Add(cmd);

            //修改仓库状态
            StringBuilder strSql4 = new StringBuilder();
            strSql4.Append("update dt_storage set ");
            strSql4.Append("storage_status=@storage_status");
            strSql4.Append(" where storage_name=@storage_name");
            SqlParameter[] parameters4 = {
                           new SqlParameter("@storage_status",SqlDbType.Int,4),
                           new SqlParameter("@storage_name",SqlDbType.NVarChar,100)};
            parameters4[0].Value = 0;
            parameters4[1].Value = GetRukuStorage(decla_id);
            cmd = new CommandInfo(strSql4.ToString(), parameters4);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        //public bool DeleteList(string idlist)
        //{
        //    List<string> sqllist = new List<string>();
        //    StringBuilder strSql2 = new StringBuilder();
        //    strSql2.Append("delete from dt_goods ");
        //    strSql2.Append(" where decla_id in (" + idlist + ")  ");
        //    sqllist.Add(strSql2.ToString());
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("delete from dt_ruku ");
        //    strSql.Append(" where id in (" + idlist + ")  ");
        //    sqllist.Add(strSql.ToString());
        //    int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
        //    if (rowsAffected > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public fcwms.Model.dt_ruku GetModel(string decla_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,customs_officer,transactor,remark,decla_id,ruku_time,ruku_way,ruku_storage,customs_broker,unstuffing_group,tallyman,customer,ruku_status from dt_ruku ");
            strSql.Append(" where decla_id=@decla_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@decla_id", SqlDbType.VarChar,100)
			};
            parameters[0].Value = decla_id;

            fcwms.Model.dt_ruku model = new fcwms.Model.dt_ruku();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                #region  父表信息
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["decla_id"] != null && ds.Tables[0].Rows[0]["decla_id"].ToString() != "")
                {
                    model.decla_id = ds.Tables[0].Rows[0]["decla_id"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ruku_time"] != null && ds.Tables[0].Rows[0]["ruku_time"].ToString() != "")
                {
                    model.ruku_time = DateTime.Parse(ds.Tables[0].Rows[0]["ruku_time"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ruku_way"] != null && ds.Tables[0].Rows[0]["ruku_way"].ToString() != "")
                {
                    model.ruku_way = ds.Tables[0].Rows[0]["ruku_way"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ruku_storage"] != null && ds.Tables[0].Rows[0]["ruku_storage"].ToString() != "")
                {
                    model.ruku_storage = ds.Tables[0].Rows[0]["ruku_storage"].ToString();
                }
                if (ds.Tables[0].Rows[0]["customs_broker"] != null && ds.Tables[0].Rows[0]["customs_broker"].ToString() != "")
                {
                    model.customs_broker = ds.Tables[0].Rows[0]["customs_broker"].ToString();
                }
                if (ds.Tables[0].Rows[0]["unstuffing_group"] != null && ds.Tables[0].Rows[0]["unstuffing_group"].ToString() != "")
                {
                    model.unstuffing_group = ds.Tables[0].Rows[0]["unstuffing_group"].ToString();
                }
                if (ds.Tables[0].Rows[0]["tallyman"] != null && ds.Tables[0].Rows[0]["tallyman"].ToString() != "")
                {
                    model.tallyman = ds.Tables[0].Rows[0]["tallyman"].ToString();
                }
                if (ds.Tables[0].Rows[0]["customer"] != null && ds.Tables[0].Rows[0]["customer"].ToString() != "")
                {
                    model.customer = ds.Tables[0].Rows[0]["customer"].ToString();
                }
                if (ds.Tables[0].Rows[0]["customs_officer"] != null && ds.Tables[0].Rows[0]["customs_officer"].ToString() != "")
                {
                    model.customs_officer = ds.Tables[0].Rows[0]["customs_officer"].ToString();
                }
                if (ds.Tables[0].Rows[0]["transactor"] != null && ds.Tables[0].Rows[0]["transactor"].ToString() != "")
                {
                    model.transactor = ds.Tables[0].Rows[0]["transactor"].ToString();
                }
                if (ds.Tables[0].Rows[0]["remark"] != null && ds.Tables[0].Rows[0]["remark"].ToString() != "")
                {
                    model.remark = ds.Tables[0].Rows[0]["remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ruku_status"] != null && ds.Tables[0].Rows[0]["ruku_status"].ToString() != "")
                {
                    model.ruku_status = int.Parse(ds.Tables[0].Rows[0]["ruku_status"].ToString());
                }
                #endregion  父表信息end

                #region  子表信息
                StringBuilder strSql2 = new StringBuilder();
                strSql2.Append("select id,storage_num,days,cost,cost_status,deal_status,remark,decla_id,goods_name,num,single_num,cube from dt_goods ");
                strSql2.Append(" where decla_id=@decla_id ");
                SqlParameter[] parameters2 = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar)};
                parameters2[0].Value = decla_id;

                DataSet ds2 = DbHelperSQL.Query(strSql2.ToString(), parameters2);
                if (ds2.Tables[0].Rows.Count > 0)
                {
                    #region  子表字段信息
                    int i = ds2.Tables[0].Rows.Count;
                    List<fcwms.Model.dt_goods> models = new List<fcwms.Model.dt_goods>();
                    fcwms.Model.dt_goods modelt;
                    for (int n = 0; n < i; n++)
                    {
                        modelt = new fcwms.Model.dt_goods();
                        if (ds2.Tables[0].Rows[n]["id"] != null && ds2.Tables[0].Rows[n]["id"].ToString() != "")
                        {
                            modelt.id = int.Parse(ds2.Tables[0].Rows[n]["id"].ToString());
                        }
                        if (ds2.Tables[0].Rows[n]["storage_num"] != null && ds2.Tables[0].Rows[n]["storage_num"].ToString() != "")
                        {
                            modelt.storage_num = decimal.Parse(ds2.Tables[0].Rows[n]["storage_num"].ToString());
                        }
                        if (ds2.Tables[0].Rows[n]["days"] != null && ds2.Tables[0].Rows[n]["days"].ToString() != "")
                        {
                            modelt.days = int.Parse(ds2.Tables[0].Rows[n]["days"].ToString());
                        }
                        if (ds2.Tables[0].Rows[n]["cost"] != null && ds2.Tables[0].Rows[n]["cost"].ToString() != "")
                        {
                            modelt.cost = decimal.Parse(ds2.Tables[0].Rows[n]["cost"].ToString());
                        }
                        if (ds2.Tables[0].Rows[n]["cost_status"] != null && ds2.Tables[0].Rows[n]["cost_status"].ToString() != "")
                        {
                            modelt.cost_status = int.Parse(ds2.Tables[0].Rows[n]["cost_status"].ToString());
                        }
                        if (ds2.Tables[0].Rows[n]["deal_status"] != null && ds2.Tables[0].Rows[n]["deal_status"].ToString() != "")
                        {
                            modelt.deal_status = int.Parse(ds2.Tables[0].Rows[n]["deal_status"].ToString());
                        }
                        if (ds2.Tables[0].Rows[n]["remark"] != null && ds2.Tables[0].Rows[n]["remark"].ToString() != "")
                        {
                            modelt.remark = ds2.Tables[0].Rows[n]["remark"].ToString();
                        }
                        if (ds2.Tables[0].Rows[n]["decla_id"] != null && ds2.Tables[0].Rows[n]["decla_id"].ToString() != "")
                        {
                            modelt.decla_id = ds2.Tables[0].Rows[n]["decla_id"].ToString();
                        }
                        if (ds2.Tables[0].Rows[n]["goods_name"] != null && ds2.Tables[0].Rows[n]["goods_name"].ToString() != "")
                        {
                            modelt.goods_name = ds2.Tables[0].Rows[n]["goods_name"].ToString();
                        }
                        if (ds2.Tables[0].Rows[n]["num"] != null && ds2.Tables[0].Rows[n]["num"].ToString() != "")
                        {
                            modelt.num = decimal.Parse(ds2.Tables[0].Rows[n]["num"].ToString());
                        }
                        if (ds2.Tables[0].Rows[n]["single_num"] != null && ds2.Tables[0].Rows[n]["single_num"].ToString() != "")
                        {
                            modelt.single_num = decimal.Parse(ds2.Tables[0].Rows[n]["single_num"].ToString());
                        }
                        if (ds2.Tables[0].Rows[n]["cube"] != null && ds2.Tables[0].Rows[n]["cube"].ToString() != "")
                        {
                            modelt.cube = decimal.Parse(ds2.Tables[0].Rows[n]["cube"].ToString());
                        }
                        models.Add(modelt);
                    }
                    model.dt_goodss = models;
                    #endregion  子表字段信息end
                }
                #endregion  子表信息end

                #region  子表信息2
                StringBuilder strSql3 = new StringBuilder();
                strSql3.Append("select id,decla_id,storage_id,shelf_id,floor_id,line_id,is_exist from dt_location ");
                strSql3.Append(" where decla_id=@decla_id ");
                SqlParameter[] parameters3 = {
					new SqlParameter("@decla_id", SqlDbType.VarChar,100)};
                parameters3[0].Value = decla_id;

                DataSet ds3 = DbHelperSQL.Query(strSql3.ToString(), parameters3);
                if (ds3.Tables[0].Rows.Count > 0)
                {
                    #region  子表字段信息
                    int i = ds3.Tables[0].Rows.Count;
                    List<fcwms.Model.dt_location> models = new List<fcwms.Model.dt_location>();
                    fcwms.Model.dt_location modelt;
                    for (int n = 0; n < i; n++)
                    {
                        modelt = new fcwms.Model.dt_location();
                        if (ds3.Tables[0].Rows[n]["id"] != null && ds3.Tables[0].Rows[n]["id"].ToString() != "")
                        {
                            modelt.id = int.Parse(ds3.Tables[0].Rows[n]["id"].ToString());
                        }
                        if (ds3.Tables[0].Rows[n]["decla_id"] != null && ds3.Tables[0].Rows[n]["decla_id"].ToString() != "")
                        {
                            modelt.decla_id = ds3.Tables[0].Rows[n]["decla_id"].ToString();
                        }
                        if (ds3.Tables[0].Rows[n]["storage_id"] != null && ds3.Tables[0].Rows[n]["storage_id"].ToString() != "")
                        {
                            modelt.storage_id = ds3.Tables[0].Rows[n]["storage_id"].ToString();
                        }
                        if (ds3.Tables[0].Rows[n]["shelf_id"] != null && ds3.Tables[0].Rows[n]["shelf_id"].ToString() != "")
                        {
                            modelt.shelf_id = ds3.Tables[0].Rows[n]["shelf_id"].ToString();
                        }
                        if (ds3.Tables[0].Rows[n]["floor_id"] != null && ds3.Tables[0].Rows[n]["floor_id"].ToString() != "")
                        {
                            modelt.floor_id = ds3.Tables[0].Rows[n]["floor_id"].ToString();
                        }
                        if (ds3.Tables[0].Rows[n]["line_id"] != null && ds3.Tables[0].Rows[n]["line_id"].ToString() != "")
                        {
                            modelt.line_id = ds3.Tables[0].Rows[n]["line_id"].ToString();
                        }
                        if (ds3.Tables[0].Rows[n]["is_exist"] != null && ds3.Tables[0].Rows[n]["is_exist"].ToString() != "")
                        {
                            modelt.is_exist = int.Parse(ds3.Tables[0].Rows[n]["is_exist"].ToString());
                        }
                        models.Add(modelt);
                    }
                    model.dt_locations = models;
                    #endregion  子表字段信息end
                }
                #endregion  子表信息end

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public fcwms.Model.dt_ruku DataRowToModel(DataRow row)
        {
            fcwms.Model.dt_ruku model = new fcwms.Model.dt_ruku();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["decla_id"] != null && row["decla_id"].ToString() != "")
                {
                    model.decla_id = row["decla_id"].ToString();
                }
                if (row["customs_broker"] != null && row["customs_broker"].ToString() != "")
                {
                    model.customs_broker = row["customs_broker"].ToString();
                }
                if (row["unstuffing_group"] != null && row["unstuffing_group"].ToString() != "")
                {
                    model.unstuffing_group = row["unstuffing_group"].ToString();
                }
                if (row["tallyman"] != null && row["tallyman"].ToString() != "")
                {
                    model.tallyman = row["tallyman"].ToString();
                }
                if (row["customer"] != null && row["customer"].ToString() != "")
                {
                    model.customer = row["customer"].ToString();
                }
                if (row["customs_officer"] != null && row["customs_officer"].ToString() != "")
                {
                    model.customs_officer = row["customs_officer"].ToString();
                }
                if (row["remark"] != null && row["remark"].ToString() != "")
                {
                    model.remark = row["remark"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,customs_officer,transactor,remark,decla_id,ruku_time,ruku_way,ruku_storage,customs_broker,unstuffing_group,tallyman,customer,ruku_status ");
            strSql.Append(" FROM dt_ruku ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by id desc");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" decla_id,customs_broker,unstuffing_group,tallyman,customer,customs_officer,remark ");
            strSql.Append(" FROM dt_ruku ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        #endregion  Method
    }
}
